import {Component, Inject, OnInit} from '@angular/core';
import {fadeInAnimation} from "../../routing/route.animation";
import {CityPickerService} from "../../shared/service/city-picker.service";
import {CityPickerColumn} from "./city-picker.model";
import {MD_DIALOG_DATA, MdDialogRef} from '@angular/material';

@Component({
  selector: 'app-city-picker',
  templateUrl: './city-picker.component.html',
  styleUrls: ['./city-picker.component.scss'],
  host: {
    '[@fadeInAnimation]': 'true'
  },
  animations: [ fadeInAnimation ]
})
export class CityPickerComponent implements OnInit {


  areaData: CityPickerColumn[]; //城市数据
  province:any={name:"福建省"};
  city:any={name:"厦门市"};
  county:any={name:"思明区"};
  code:string="350203"; //最后选择的城市编码
  SWIPE_ACTION = {  UP: 'swipeup', DOWN: 'swipedown',LEFT: 'swipeleft', RIGHT: 'swiperight' };
  PAN_ACTION = {  UP: 'panup', DOWN: 'pandown',LEFT: 'panleft', RIGHT: 'panright' };

  provinceNum:any=1;
  citynum:any=0;
  countynum:any=0;

  config:any=[]
  constructor(  public cityPickerSev: CityPickerService,public dialogRef: MdDialogRef<CityPickerComponent>,@Inject(MD_DIALOG_DATA) data: any) {
    this.config = data
  }

  ngOnInit() {
    // console.log(this.config);
    this.province = {name:this.config.province};
    this.city = {name:this.config.city};
    this.county = {name:this.config.county};
    this.setCityPickerData();
  }
  /**
   * 获取城市数据
   */
  setCityPickerData(){
    this.cityPickerSev.getCitiesData()
      .then( data => {
        this.areaData = data;
        // console.log(this.areaData)
        for (let i = 0; i<this.areaData.length; i++) {
          let area = this.areaData[i];
          this.provinceNum = i;
          if(area.name == this.province.name){
            this.province = area;
            break;
          }
        }
        for (let i = 0; i<this.province.children.length; i++) {
          let area = this.province.children[i];
          this.citynum = i;
          if(area.name == this.city.name){
            this.city = area;
            break;
          }
        }
        for (let i = 0; i<this.city.children.length; i++) {
          let area = this.city.children[i];
          this.countynum = i;
          if(area.name == this.county.name){
            this.county = area;
            this.code = this.county.code;
            break;
          }
        }
      });
  }

  selectProvince(e){
    let nextIndex = 0;
    if(e.type === this.SWIPE_ACTION.DOWN || e.type === this.PAN_ACTION.DOWN) {
      const isFirst = this.provinceNum === 0;
      // nextIndex = isFirst ? this.areaData.length - 1 : this.provinceNum - 1;
      nextIndex = isFirst ? 0: this.provinceNum - 1;
    }
    if(e.type === this.SWIPE_ACTION.UP || e.type === this.PAN_ACTION.UP) {
      const isLast = this.provinceNum === this.areaData.length - 1;
      nextIndex = isLast ? this.areaData.length - 1 : this.provinceNum + 1;
      // nextIndex = isLast ? 1 : this.provinceNum + 1;
    }

    this.citynum = 0;
    this.countynum=0
    this.provinceNum = nextIndex;
    this.province = this.areaData[this.provinceNum];
    this.city = this.province.children[this.citynum];
    this.county = this.city.children[this.countynum];
    this.code = this.county.code;
    // console.log(this.county)
  }
  selectCity(e){
    let nextIndex = 0;
    if(e.type === this.SWIPE_ACTION.DOWN || e.type === this.PAN_ACTION.DOWN) {
      const isFirst = this.citynum === 0;
      nextIndex = isFirst ? 0 : this.citynum - 1;
    }
    if(e.type === this.SWIPE_ACTION.UP || e.type === this.PAN_ACTION.UP) {
      const isLast = this.citynum === this.province.children.length - 1;
      nextIndex = isLast ? this.province.children.length-1 : this.citynum + 1;
    }

    this.countynum=0
    this.citynum = nextIndex;
    this.city = this.province.children[this.citynum];
    this.county = this.city.children[this.countynum];
    this.code = this.county.code;
    // console.log(  this.county)
  }
  selectCounty(e){
    let nextIndex = 0;
    if(e.type === this.SWIPE_ACTION.DOWN || e.type === this.PAN_ACTION.DOWN) {
      const isFirst = this.countynum === 0;
      nextIndex = isFirst ? 0 : this.countynum - 1;
    }
    if(e.type === this.SWIPE_ACTION.UP || e.type === this.PAN_ACTION.UP) {
      const isLast = this.countynum === this.city.children.length - 1;
      nextIndex = isLast ? this.city.children.length - 1 : this.countynum + 1;
    }

    this.countynum = nextIndex;
    this.county = this.city.children[this.countynum];
    this.code = this.county.code;
    // console.log(this.county)
  }

  /**
   * 热门城市快速定位
   *
   * @param e
   * @param code 城市code
   */
  fastPositioning(e,code){
    let provinceCode = code.substring(0,2)+"0000";
    for (let i = 0; i<this.areaData.length; i++) {
      let area = this.areaData[i];
      this.provinceNum = i;
      if(area.code == provinceCode){
        this.province = area;
        break;
      }
    }
    for (let i = 0; i<this.province.children.length; i++) {
      let area = this.province.children[i];
      this.citynum = i;
      if(area.code == code){
        this.city = area;
        break;
      }
    }
    this.countynum = 0;
    this.county = this.city.children[0];
    this.code = this.county.code;
  }

}
